/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.controller.event;

import com.common.json.EventLibraryJson;
import com.entities.EventInfo;
import com.entities.EventLibrary;
import com.model.EventLibraryModel;
import java.io.UnsupportedEncodingException;
import java.util.Locale;
import javax.enterprise.context.RequestScoped;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import static javax.management.Query.value;
import javax.servlet.http.HttpServletRequest;
import org.json.simple.JSONObject;

/**
 *
 * @author Tuan Long
 */
@Named(value = "createEvent")
@RequestScoped
public class createEvent {

    public createEvent() {
    }
    /**
     * @createEvent.getJsonEventMessage: get JsonObject data from input values
     * @return JsonObject
     */
    private String messagError;

    public String getMessagError() {
        return messagError;
    }

    public void setMessagError(String messagError) {
        this.messagError = messagError;
    }
    private String avatar;
    private String shortdes;
    private String address;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    public String getShortdes() {
        return shortdes;
    }

    public void setShortdes(String shortdes) {
        this.shortdes = shortdes;
    }

    private JSONObject getJsonEventMessage()   {
        HttpServletRequest req = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        String eventName = req.getParameter("eventName");
        String address = req.getParameter("formevent:formhehe:acSimple_input");
        String des = req.getParameter("description");
        String type = req.getParameter("eventType");
        String dateS = req.getParameter("timeS");
        String dateE = req.getParameter("timeE");
        String timepS = req.getParameter("timepS");
        String timepE = req.getParameter("timepE");
        String start = dateS + " " + timepS;
        String end = dateE + " " + timepE;
        avatar = req.getParameter("avatar");
        shortdes = req.getParameter("shortdes");
        EventInfo ei = new EventInfo();
        ei.setAvatar(avatar);
        ei.setShortdes(shortdes);
        ei.setAddress(address);
        //event ID set default : 0. Because field eventID from DB is primary key with Identity(1,1)
        EventLibrary event = new EventLibrary(0, eventName, des, type, start, end);
        JSONObject json = EventLibraryJson.convertEventLibraryAndInfoDataToJsonObject(event, ei);
        return json;
    }

    /**
     * @createEvent.createNewEvent: use EJB Remote insert new record
     * EventLibrary into DB
     * @return String
     */
    public String createNewEvent()   {
        JSONObject json = getJsonEventMessage();
        String jsonstr = json.toString();
        EventLibraryModel model = new EventLibraryModel();
        boolean b = model.createNewEvent(jsonstr);
        if (b) {
            return "events?faces-redirect=true";
        }
        return "error?faces-redirect=true";
    }

}
